package com.dotflat.check.controller;

import com.dotflat.check.domain.DeviceRecord;
import com.dotflat.check.po.*;
import com.dotflat.check.service.DeviceRecordService;
import com.dotflat.rest.controller.RestfulBaseController;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.service.ClassesService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

import static com.matt.foundation.constant.RequestConstant.TICKET;


/**
 * 巡检字典Controller
 *
 * @author #
 * @date 2021-11-25
 */
@RestController
@RequestMapping("/api/device_record")
@Slf4j
@Api(tags = "设备记录")
public class DeviceRecordController extends RestfulBaseController<DeviceRecord, DeviceRecordService, DeviceRecordPO>
{

    @Autowired
    private  DeviceRecordService deviceRecordService;

    @Autowired
    protected MemVerificationCache cache;

    @Autowired
    private ClassesService classesService;

    @Autowired
    public void setBaseService(DeviceRecordService service) {
        super.setBaseService(service);
    }



    @ApiOperation(value = "最新 - 记录")
    @PostMapping("/newest_list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = DeviceRecordPO.class),
    })
    public StatusResponse newestList(@RequestHeader(TICKET) String ticket){
        StaffDTO staffDTO = cache.getStaff(ticket);
        if (Objects.isNull(staffDTO)){
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        List<DeviceRecord>  list = deviceRecordService.newestList( staffDTO.getAreaId());
        return StatusResponse.success(list);
    }
}
